use std::collections::HashMap;
use std::sync::LazyLock;

static EN_JSON: &str = include_str!("../assets/locales/en.json");
static ZH_JSON: &str = include_str!("../assets/locales/zh.json");

static RESOURCES: LazyLock<HashMap<&'static str, HashMap<String, String>>> = LazyLock::new(|| {
    let mut m: HashMap<&'static str, HashMap<String, String>> = HashMap::new();
    let en: HashMap<String, String> =
        serde_json::from_str(EN_JSON).expect("failed to parse en.json");
    let zh: HashMap<String, String> =
        serde_json::from_str(ZH_JSON).expect("failed to parse zh.json");
    m.insert("en", en);
    m.insert("zh", zh);
    m
});

/// Translate a key for the given language code (e.g. "zh", "en").
/// Falls back to "en" then to the key itself.
#[must_use]
pub fn t(lang: &str, key: &str) -> String {
    RESOURCES
        .get(lang)
        .and_then(|map| map.get(key).cloned())
        .or_else(|| RESOURCES.get("en").and_then(|map| map.get(key).cloned()))
        .unwrap_or_else(|| key.to_string())
}

/// Convenience: fallback language code used by components when none provided.
#[must_use]
pub fn default_lang() -> &'static str {
    "zh"
}

/// 获取支持的语言列表
#[must_use]
pub fn supported_languages() -> Vec<(&'static str, &'static str)> {
    vec![("zh", "中文"), ("en", "English")]
}
